Random


In [ ]:

choices: A weighted version o random

Python >= 3.6


In [78]:
from random import choices

lnct_few_friends = ["Jyoti Pancholi", "Amit Shrivastava", "Mukesh Bansal", "Preeti Saraswat", "Manish Nandle"]
list_of_prob = [0.2, 0.1, 0.3, 0.2, 0.2]
lnct_few_friends = choices(lnct_few_friends, weights=list_of_prob, k=200)
for name in set(population):
    print(name, lnct_few_friends.count(name))


Preeti Saraswat 40
Jyoti Pancholi 38
Mukesh Bansal 57
Manish Nandle 35
Amit Shrivastava 30

Lets try some graphs on them


In [45]:
from random import choices
import matplotlib.pyplot as plt

lnct_few_friends = ["X", "Jyoti Pancholi", "Amit Shrivastava", "Mukesh Bansal", "Preeti Saraswat", "Manish Nandle"]
list_of_prob = [0.05, 0.15, 0.1, 0.3, 0.1, 0.3]
d = {}
for i in range(10):
    a = {}
    lst = choices(lnct_few_friends, weights=list_of_prob, k=10)
    for name in set(lnct_few_friends):
        a[name] = lst.count(name)
    d[i] = a
# print(d)

import matplotlib.pyplot as plt
from  matplotlib import ticker
import math
plt.xticks(rotation=90)
for key, val in d.items():
    plt.plot(val.keys() ,val.values(), marker="o")


In the above graph, you can see that everytime, "X" were the lowest and "Mukesh" & "Manish" were the highests names in the created list and more I increase the value of k more prominent the graph becomes as shown in the below code and graph


In [53]:
from random import choices
import matplotlib.pyplot as plt

lnct_few_friends = ["X", "Jyoti Pancholi", "Amit Shrivastava", "Mukesh Bansal", "Preeti Saraswat", "Manish Nandle"]
list_of_prob = [0.05, 0.15, 0.1, 0.3, 0.1, 0.3]
d = {}
for i in range(10):
    a = {}
    lst = choices(lnct_few_friends, weights=list_of_prob, k=900)
    for name in set(lnct_few_friends):
        a[name] = lst.count(name)
    d[i] = a

import matplotlib.pyplot as plt
from  matplotlib import ticker
import math
plt.xticks(rotation=90)
for key, val in d.items():
    plt.plot(val.keys() ,val.values(), marker="o")